<?

Class TVRage_Show {

  public static function getShow ($seriesName) {
    $myTVRage = new TVRage_Show($seriesName);

    // get whatever's available from TVRage
    if ($myTVRage->getInfo() === true)
      return $myTVRage;
    else
      return null;
    
  }

  public $seriesName;
  public $nextSeason;
  public $nextEpisode;
  public $nextAirdate;
  public $nextEpName;
  public $lastSeason;
  public $lastEpisode;
  public $lastAirdate;
  public $lastEpName;
  public $status;
  public $airs;
  public $network;
  
  function __construct ($seriesName) {
    $this->nextSeason = false;
    $this->nextEpisode = false;
    $this->nextAirdate = false;
    $this->nextEpName = false;
    $this->lastSeason = false;
    $this->lastEpisode = false;
    $this->lastAirdate = false;
    $this->lastEpName = false;
    $this->status = false;
    $this->airs = false;
    $this->network = false;

    $this->seriesName = $seriesName;
   
  }
  
  // get the episode that airs next from tvrage.com
  function getInfo () {
    global $config;
    
    if (empty($config['curl_timeout']))
      $timeout = 30;
    else
      $timeout = $config['curl_timeout'];


    $ch = curl_init('http://www.tvrage.com/quickinfo.php?show=' . urlencode($this->seriesName) . '&exact=0');
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    
    $response = curl_exec($ch);
    
    $httpCode = curl_getinfo($ch ,CURLINFO_HTTP_CODE);
    $headerSize = curl_getinfo($ch,CURLINFO_HEADER_SIZE);
    $data = substr( $response, $headerSize );
    curl_close($ch);

    if($httpCode == 200) {

      //get all info for the show
      foreach (explode("\n", $data) as $line) {
      
        list($prop, $val) = explode('@', trim($line), 2);

        switch($prop) {
          case 'Latest Episode':
            list ($ep, $title, $airdate) = explode('^', $val);
            $episode = explode('x', $ep);
            $this->lastSeason = (int)$episode[0];
            $this->lastEpisode = (int)$episode[1];
            $this->lastAirdate = strtotime(str_replace("/", " ", $airdate));
            $this->lastEpName = $title;
            break;
          case 'Next Episode':
            list ($ep, $title, $airdate) = explode('^', $val);
            $episode = explode('x', $ep);
            $this->nextSeason = (int)$episode[0];
            $this->nextEpisode = (int)$episode[1];
            $this->nextAirdate = strtotime(str_replace("/", " ", $airdate));
            $this->nextEpName = $title;
            break;
          case 'Airtime':
            $this->airs = $val;
            break;
          case 'Network':
            $this->network = $val;
            break;
          case 'Status':
            if (stristr($val, 'ended') !== false)
              $this->status = 'Ended';
            else
              $this->status = 'Continuing';
            break;
        }
      }

      return true;
    }
    else {
      return false;
    }
  }
  
  function comingEpisode ($curSeason=1, $curEpisode=0) {

    if ($this->nextSeason === false || $this->nextEpisode === false)
      return false;
  
    if ( ($this->nextSeason == $curSeason && $this->nextEpisode > $curEpisode) || ($this->nextSeason > $curSeason) )
      return array('season' => $this->nextSeason, 'episode' => $this->nextEpisode, 'airdate' => $this->nextAirdate, 'name' => $this->nextEpName);

    // if not it's probably outdated info or something, don't use it
    else
      return false;

  }
    
}

?>